package org.hiucung.orangeappupdater.updater.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;

import androidx.core.content.FileProvider;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;

/***
 * 应用工具类
 * 1、获取当前应用的版本号
 * 2、安装apk应用
 * 3、检查更新应用安装包的md5值
 */
public class AppUtils {

    //获取当前应用的版本号
    public static long getVersionCode(Context context) {
        PackageManager packageManager =
                context.getPackageManager();

        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
                return packageInfo.getLongVersionCode();
            }else {
                return packageInfo.versionCode;
            }

        }catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
        }
        return -1;
    }

    //安装apk应用的方法，需要注意各个安卓版本的适配
    public static void installApk(Activity activity, File apkFile) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = null;

        //Android N FileProvider :android.os.FileUriExposedException:
        //1、manifest
        //2、xml
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(activity,activity.getPackageName() + ".fileprovider",apkFile);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//读写权限都赋予上
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }else {
            uri = Uri.fromFile(apkFile);
        }
        intent.setDataAndType(uri,"application/vnd.android.package-archive");
        activity.startActivity(intent);

        //Android O INSTALL PERMISSION的适配
        //android.permission.REQUEST_INSTALL_PACKAGES
    }

    //检查更新应用安装包的md5值
    public static String getFileMd5(File targetFile) {
        if (targetFile == null || !targetFile.isFile()){
            return null;
        }
        MessageDigest digest = null;
        FileInputStream inputStream = null;
        byte[] buffer = new byte[1024];
        int len = 0;
        try {
            digest = MessageDigest.getInstance("MD5");
            inputStream = new FileInputStream(targetFile);
            while ((len = inputStream.read(buffer)) != -1){
                digest.update(buffer,0,len);
            }

        } catch (Exception e){
            e.printStackTrace();
        }finally {
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        byte[] result = digest.digest();
        BigInteger bigInteger = new BigInteger(1,result);
        //使用BigInteger转16进制
        return bigInteger.toString(16);
    }
}
